﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectBot.Managers;

namespace ProjectBot.Controls
{
    public partial class CommandBox : UserControl
    {
        private List<string> PreviousConsoleCommands = new List<string>();

        public CommandBox()
        {
            InitializeComponent();
        }

        private void CommandBox_Load(object sender, EventArgs e)
        {
            this.txt_CommandBox.KeyDown += new KeyEventHandler(txt_CommandBox_KeyDown);
        }
        #region Events
        private void txt_CommandBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                // if this line exists we are typing it again, so move it to the recent spot
                if (txt_CommandBox.Text != string.Empty) { AddRecentCommand(txt_CommandBox.Text); }
                this.btn_Send_Click(sender, e);
            }
            if (e.KeyCode == Keys.Up)
            {
                try
                {
                    txt_CommandBox.Text = PreviousConsoleCommands[GetCurrentCommandSelection(txt_CommandBox.Text) - 1];
                }
                catch { }
            }
            if (e.KeyCode == Keys.Down)
            {
                if (txt_CommandBox.Text == string.Empty)
                {
                    try
                    {
                        txt_CommandBox.Text = PreviousConsoleCommands[PreviousConsoleCommands.Count];
                    }
                    catch { }
                }
                // Get the current spot in our list
                try
                {
                    txt_CommandBox.Text = PreviousConsoleCommands[GetCurrentCommandSelection(txt_CommandBox.Text) + 1];
                }
                catch { }
            }
        }
        private int GetCurrentCommandSelection(string command)
        {
            int reply = 0;
            for (int i = 0; i < PreviousConsoleCommands.Count; i++)
            {
                if (command.ToLower() == PreviousConsoleCommands[i].ToLower())
                {
                    reply = i;
                }
            }
            return reply;
        }
        public void AddRecentCommand(string command)
        {
            int index = GetCurrentCommandSelection(command);
            try
            {
                PreviousConsoleCommands.Remove(txt_CommandBox.Text);
                PreviousConsoleCommands.Add(txt_CommandBox.Text);
            }
            catch { ProjectBotForm.WriteLine("Error in addingrecent", Color.Red); }
        }
        private void btn_Send_Click(object sender, EventArgs e)
        {
            //// get the string of the command
            if (!PreviousConsoleCommands.Contains(txt_CommandBox.Text))
            {
                PreviousConsoleCommands.Add(txt_CommandBox.Text);
            }
            // Break it down to an array
            string[] args = txt_CommandBox.Text.Split(' ');
            // Run our switch
            if (args.Length > 0)
            {
                CommandManager.Manage(this,args);
            }
        }
        #endregion
    }
}
